//
//  PlanController.m
//  Water
//
//  Created by yh on 2017/11/27.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "PlanController.h"
#import "PlanDetailController.h"
#import "PlanItemCell.h"
#import "PlanHeaderView.h"
#import "PlanDefultHeaderView.h"
#import "CalculateVC.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <YYKit/UITableView+YYAdd.h>
@interface PlanController ()<PlanItemCellDelegate>

@end

@implementation PlanController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
    
    
}

- (void)setEditbuttonWithstyle:(UIBarButtonSystemItem)style{
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc]bk_initWithBarButtonSystemItem:style handler:^(id sender) {
        [self.tableView setEditing:style == UIBarButtonSystemItemEdit animated:YES];
        [self setEditbuttonWithstyle:style == UIBarButtonSystemItemEdit ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit];
    }];
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]bk_initWithBarButtonSystemItem:UIBarButtonSystemItemAdd handler:^(id sender) {
        PlanDetailController *planDetailController = [[PlanDetailController alloc]init];
        planDetailController.title = @"新增计划";
        planDetailController.save = ^(PlanItem *plan) {
            [[AppClient shared] addPlan:plan];
            [[AppClient shared] savePlans];
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:planDetailController animated:YES];
    }];
    
    self.navigationItem.rightBarButtonItems = @[addBtn,editBtn];
}

- (void)setupViews{
    self.tableView.allowsSelectionDuringEditing=YES;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PlanItemCell" bundle:nil] forCellReuseIdentifier:@"PlanItemCell"];
    [self setEditbuttonWithstyle:UIBarButtonSystemItemEdit];
    [self.tableView registerNib:[UINib nibWithNibName:@"PlanDefultHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"PlanDefultHeaderView"];
    PlanHeaderView *header = WaterXIB(@"PlanHeaderView");
    [header.calculateBtn addTarget:self action:@selector(calculateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = header;
}


- (void)calculateBtnAction:(UIButton *)sender{
    CalculateVC *calculateVC =  [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CalculateVC"];
    calculateVC.hidesBottomBarWhenPushed = YES;
    calculateVC.title = @"水量计算";
    [self.navigationController pushViewController:calculateVC animated:YES];
}

- (void)setDefultSwitchAction:(UISwitch *)btn{
    [[AppClient shared] useDefultPlans:btn.isOn];
    [self.tableView reloadData];
    
}

#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [AppClient shared].plans.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlanItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlanItemCell" forIndexPath:indexPath];
    cell.model =  [AppClient shared].plans[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PlanItem *item = [AppClient shared].plans[indexPath.row];
    PlanDetailController *planDetailController = [[PlanDetailController alloc]init];
    planDetailController.title = @"编辑计划";
    planDetailController.plan = item;
    planDetailController.save = ^(PlanItem *plan) {
        [[AppClient shared] savePlans];
        [self.tableView reloadData];
    };
    
    [self.navigationController pushViewController:planDetailController animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    [tableView dequeueReusableHeaderFooterViewWithIdentifier:@""];
    
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    PlanDefultHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PlanDefultHeaderView"];
    [view.setDefultSwitch addTarget:self action:@selector(setDefultSwitchAction:) forControlEvents:UIControlEventValueChanged];
    return view;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    PlanItem *item = [AppClient shared].plans[indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[AppClient shared] deletePlan:item];
        [tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)planItemCell:(PlanItemCell *)planItemCell didSwitch:(UISwitch *)switchBtn isSelect:(BOOL)isSelect model:(PlanItem *)model{
    model.isRemind = isSelect;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
